/*** Eclipse Class Decompiler plugin, copyright (c) 2012 Chao Chen (cnfree2000@hotmail.com) ***/
package com.qihoo360.mobilesafe.utils;

import android.content.Context;
import android.content.res.AssetManager;
import android.text.TextUtils;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;

public class AssetsUtils {
    private static final String TAG = "FWL.AssetsUtils";

    public static final String readUTF8(Context paramContext, String paramString) {
        FileInputStream localFileInputStream = null;
        try {
            File localFile = new File(paramString);
            localFileInputStream = new FileInputStream(localFile);
            String str = IoStreamUtils.readUTF8(localFileInputStream);
            return str;
        } catch (Throwable localThrowable) {} finally {
            IoStreamUtils.closeSilently(localFileInputStream);
        }
        return null;
    }

    public static final boolean checkMD5(Context paramContext, String paramString1, String paramString2,
            String paramString3) {
        String str1 = readUTF8(paramContext, paramString2 + File.separator + paramString3 + ".md5");
        if (TextUtils.isEmpty(str1))
            return false;
        str1 = str1.trim();
        str1 = str1.toUpperCase();
        if (TextUtils.isEmpty(str1))
            return false;
        FileInputStream localFileInputStream = null;
        try {
            localFileInputStream = new FileInputStream(paramString2 + "/" + paramString3);
            byte[] arrayOfByte = IoStreamUtils.readMD5(localFileInputStream);
            if ((arrayOfByte == null) || (arrayOfByte.length <= 0))
                return false;
            String str2 = StringUtils.toHexString(arrayOfByte);
            if (TextUtils.isEmpty(str2))
                return false;
            boolean bool = str1.equals(str2);
            return bool;
        } catch (Throwable localThrowable) {} finally {
            IoStreamUtils.closeSilently(localFileInputStream);
        }
        return false;
    }

    public static final boolean extractTo(Context paramContext, String paramString1, String paramString2,
            String paramString3, byte[] paramArrayOfByte) {
        File localFile = new File(paramString2 + "/" + paramString3);
        InputStream localInputStream = null;
        FileOutputStream localFileOutputStream = null;
        try {
            localInputStream = paramContext.getAssets().open(paramString1);
            localFileOutputStream = new FileOutputStream(localFile);
            byte[] arrayOfByte = new byte[4096];
            int i = 0;
            if ((paramArrayOfByte != null) && (paramArrayOfByte.length > 0)) {
                int j = 0;
                while ((i = localInputStream.read(arrayOfByte)) >= 0) {
                    if (i <= 0)
                        continue;
                    for (int k = 0; k < i; ++k) {
                        int tmp101_99 = k;
                        byte[] tmp101_97 = arrayOfByte;
                        tmp101_97[tmp101_99] = (byte) (tmp101_97[tmp101_99] ^ paramArrayOfByte[(j++)]);
                        if (j < paramArrayOfByte.length)
                            continue;
                        j = 0;
                    }
                    localFileOutputStream.write(arrayOfByte, 0, i);
                }
            }
            else {
                do {
                    if (i <= 0)
                        continue;
                    localFileOutputStream.write(arrayOfByte, 0, i);
                } while ((i = localInputStream.read(arrayOfByte)) >= 0);
            }
            return true;
        } catch (Throwable localThrowable) {} finally {
            IoStreamUtils.closeSilently(localInputStream);
            IoStreamUtils.closeSilently(localFileOutputStream);
        }
        return false;
    }

    public static final boolean quickExtractTo(Context paramContext, String paramString1, String paramString2,
            String paramString3, boolean paramBoolean, byte[] paramArrayOfByte) {
        File localFile = new File(paramString2 + "/" + paramString3);
        if (!(Utils.checkFileDir(paramContext)))
            return false;
        if (localFile.exists()) {
            if ((paramBoolean) && (checkMD5(paramContext, paramString1, paramString2, paramString3)))
                return true;
            if (!(localFile.delete()))
                return false;
            if (localFile.exists())
                return false;
        }
        new ZipUpdate(paramContext).copyAssetToFile();
        int i = 0;
        if (localFile.exists())
            i = 1;
        return true;
    }
}